# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: OSAMC <https://github.com/osam-cologne/archlinux-proaudio>
# Contributor: Bleuzen <supgesu at gmail dot com>
# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Florian Hülsmann <fh@cbix.de>

pkgname=surge-xt
pkgver=1.1.2
pkgrel=1
pkgdesc='Subtractive hybrid synthesizer'
arch=('loong64' 'x86_64')
url='https://surge-synthesizer.github.io'
license=('GPL3')
groups=('pro-audio' 'vst3-plugins' 'clap-plugins' 'lv2-plugins')
depends=(
  'glibc'
  'gcc-libs'
  'cairo'
  'fontconfig'
  'freetype2'
  'libx11'
  'libxcb'
  'libxkbcommon-x11'
  'xcb-util'
  'xcb-util-cursor'
  'xcb-util-image'
  'xcb-util-keysyms'
  'xcb-util-renderutil'
  'xdg-utils'
  'hicolor-icon-theme'
  'fmt'
  'libsamplerate'
)
makedepends=(
  'git'
  'cmake'
  'libsndfile'
  'python'
  'libxrandr'
  'libxinerama'
  'libxcursor'
  'alsa-lib'
  'jack'
  'webkit2gtk'
  'ripgrep'
  'lv2'
  'juce'
)
optdepends=(
  'jack: backend for standalone'
  'alsa-lib: backend for standalone'
  'vst3-host: for VST3 plugins'
  'clap-host: for CLAP plugins'
  'lv2-host: for LV2 plugins'
)
options=('!lto' 'debug') # LTO disabled due to crashes at application startup
_commit='d3568a9ae6c8555590e06e55ed9cc6965a939f91'
source=(
  "$pkgname::git+https://github.com/surge-synthesizer/surge#commit=$_commit"
  'improve-reproducibility.patch'
  'devendor-libs.patch'
  'fix-lv2-ftbfs.patch'
  'github.com-surge-synthesizer-tuning-library::git+https://github.com/surge-synthesizer/tuning-library'
  'github.com-surge-synthesizer-eurorack::git+https://github.com/surge-synthesizer/eurorack'
  'github.com-surge-synthesizer-JUCE::git+https://github.com/surge-synthesizer/JUCE'
  'github.com-surge-synthesizer-sst-plugininfra::git+https://github.com/surge-synthesizer/sst-plugininfra'
  'github.com-surge-synthesizer-sst-cpputils::git+https://github.com/surge-synthesizer/sst-cpputils'
  'github.com-surge-synthesizer-sst-filters::git+https://github.com/surge-synthesizer/sst-filters'
  'github.com-surge-synthesizer-sst-waveshapers::git+https://github.com/surge-synthesizer/sst-waveshapers'
  'github.com-free-audio-clap::git+https://github.com/free-audio/clap'
  'github.com-free-audio-clap-helpers::git+https://github.com/free-audio/clap-helpers'
  'github.com-free-audio-clap-juce-extensions::git+https://github.com/free-audio/clap-juce-extensions'
  'github.com-simd-everywhere-simde::git+https://github.com/simd-everywhere/simde'
  'github.com-ODDSound-MTS-ESP::git+https://github.com/ODDSound/MTS-ESP'
  'github.com-LuaJIT-LuaJIT::git+https://github.com/LuaJIT/LuaJIT'
  'github.com-taocpp-PEGTL::git+https://github.com/taocpp/PEGTL'
  'github.com-gulrak-filesystem::git+https://github.com/gulrak/filesystem'
  'github.com-lv2-porting-project-JUCE::git+https://github.com/lv2-porting-project/JUCE#branch=lv2'
)
b2sums=('SKIP'
        '6e71b56ffde699319def99e03637d9c3fb6197a6e0637b9f44b52a9207a8e06c8d482c05cdd711ec2c5901f403381dda70e786bca6b56cce14dc449325f24761'
        '6bbeef320c380fb8bf9339add847bf7db237a2f93f544c2237e2d51ef9a1f497916539ace523c9b0911acabc755d872d5318170887aa62b27654eee40de6b693'
        'c43e5a1b9605243be07ab3903885836a2199787f0c7a8dc6ae2be9d8d4a91bcf07fde520c3440f1be425194476065cb287b99446b144c5cd76d012fb0f10f35e'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^release_xt_//'
}

prepare() {
  cd "$pkgname"

  # explicitly state which submodules to use,
  # omitting devendored & unused submodules
  git submodule init \
    libs/tuning-library \
    libs/simde \
    libs/eurorack/eurorack \
    libs/oddsound-mts/MTS-ESP \
    libs/JUCE \
    libs/LuaJitLib/LuaJIT \
    libs/PEGTL \
    libs/sst/sst-plugininfra \
    libs/sst/sst-cpputils \
    libs/clap-juce-extensions \
    libs/sst/sst-filters \
    libs/sst/sst-waveshapers

  git config submodule.libs/tuning-library.url "$srcdir/github.com-surge-synthesizer-tuning-library"
  git config submodule.libs/simde.url "$srcdir/github.com-simd-everywhere-simde"
  git config submodule.libs/eurorack/eurorack.url "$srcdir/github.com-surge-synthesizer-eurorack"
  git config submodule.libs/oddsound-mts/MTS-ESP.url "$srcdir/github.com-ODDSound-MTS-ESP"
  git config submodule.libs/JUCE.url "$srcdir/github.com-surge-synthesizer-JUCE"
  git config submodule.libs/LuaJitLib/LuaJIT.url "$srcdir/github.com-LuaJIT-LuaJIT"
  git config submodule.libs/PEGTL.url "$srcdir/github.com-taocpp-PEGTL"
  git config submodule.libs/sst/sst-plugininfra.url "$srcdir/github.com-surge-synthesizer-sst-plugininfra"
  git config submodule.libs/sst/sst-cpputils.url "$srcdir/github.com-surge-synthesizer-sst-cpputils"
  git config submodule.libs/clap-juce-extensions.url "$srcdir/github.com-free-audio-clap-juce-extensions"
  git config submodule.libs/sst/sst-filters.url "$srcdir/github.com-surge-synthesizer-sst-filters"
  git config submodule.libs/sst/sst-waveshapers.url "$srcdir/github.com-surge-synthesizer-sst-waveshapers"

  git -c protocol.file.allow=always submodule update

  # setup git submodules for clap-juce-extensions
  pushd libs/clap-juce-extensions
  git submodule init
  git config submodule.clap-libs/clap.url "$srcdir/github.com-free-audio-clap"
  git config submodule.clap-libs/clap-helpers.url "$srcdir/github.com-free-audio-clap-helpers"
  git -c protocol.file.allow=always submodule update
  popd

  # setup git submodules for sst-plugininfra
  pushd libs/sst/sst-plugininfra
  git submodule init
  git config submodule.libs/filesystem/filesystem.url "$srcdir/github.com-gulrak-filesystem"
  git -c protocol.file.allow=always submodule update
  popd

  # first shot at improving reproducibility:
  # https://github.com/surge-synthesizer/surge/issues/2389
  patch -p1 -i "$srcdir/improve-reproducibility.patch"

  # devendor libs
  patch -p1 -i "$srcdir/devendor-libs.patch"
  rg --files-with-matches 'include "fmt/core.h"' | xargs -I@ sed 's|"fmt/core.h"|<fmt/core.h>|g' -i @
  rg --files-with-matches 'include "samplerate.h"' | xargs -I@ sed 's|"samplerate.h"|<samplerate.h>|g' -i @

  # add missing LV2 header
  cd "$srcdir/github.com-lv2-porting-project-JUCE"
  patch -p1 -i "$srcdir/fix-lv2-ftbfs.patch"
}

build() {
  # binaries and shared libs have executable stack:
  # https://github.com/surge-synthesizer/surge/issues/1701
  export LDFLAGS="$LDFLAGS,-z,noexecstack"

  cmake \
    -B build \
    -S "$pkgname" \
    -DCMAKE_INSTALL_PREFIX='/usr' \
    -DCMAKE_INSTALL_LIBDIR='/usr/lib' \
    -DCMAKE_BUILD_TYPE=Release \
    -W no-dev \
    -DSURGE_BUILD_TESTRUNNER=OFF

  cmake --build build

  # build LV2 plugin
  cmake \
    -B build-lv2 \
    -S "$pkgname" \
    -DCMAKE_INSTALL_PREFIX='/usr' \
    -DCMAKE_INSTALL_LIBDIR='/usr/lib' \
    -DCMAKE_BUILD_TYPE=Release \
    -W no-dev \
    -DSURGE_BUILD_TESTRUNNER=OFF \
    -DJUCE_SUPPORTS_LV2=True \
    -DSURGE_JUCE_PATH="$srcdir/github.com-lv2-porting-project-JUCE"

  cmake --build build-lv2 --target surge-xt_LV2 surge-fx_LV2 --parallel
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  # LV2 plugin
  install -vd "$pkgdir/usr/lib/lv2"
  cp -vr build-lv2/surge_xt_products/*.lv2 "$pkgdir/usr/lib/lv2"

  cd "$pkgname"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" AUTHORS README.md doc/*

  # application icons & desktop files
  install -vd "$pkgdir/usr/share/"{applications,icons}
  cd scripts/installer_linux
  cp -r assets/icons/hicolor "$pkgdir/usr/share/icons"
  cp -r assets/icons/scalable "$pkgdir/usr/share/icons/hicolor"
  install -vDm644 -t "$pkgdir/usr/share/applications" assets/applications/*.desktop
}
